﻿using OtpNet;
using System;
using System.Collections.Generic;
using System.Text;
using Yhd.Captcha.Core.Helpers;
using Volo.Abp.DependencyInjection;

namespace Yhd.Captcha.Core.OtpNet
{
    public class YhdTotp
    {
        private readonly Totp totp;

        public YhdTotp(byte[] secretKey, int step = 60, OtpHashMode mode = OtpHashMode.Sha1, int totpSize = 6, TimeCorrection timeCorrection = null)
        {
            totp = new Totp(secretKey, step, mode, totpSize, timeCorrection);
        }

        public YhdTotp(int step = 60, OtpHashMode mode = OtpHashMode.Sha1, int totpSize = 6, TimeCorrection timeCorrection = null)
        {
            var secretKey = Base32Helper.GetBase32Bytes(20);
            totp = new Totp(secretKey, step, mode, totpSize, timeCorrection);
        }

        public string Generate()
        {
            return totp.ComputeTotp(DateTime.UtcNow);
        }

        public bool VerifyTotp(string totpCode, out long timeWindowUsed, VerificationWindow window = null)
        {
            return totp.VerifyTotp(totpCode, out timeWindowUsed, window);
        }
        public bool VerifyTotp(DateTime timestamp, string totpCode, out long timeWindowUsed, VerificationWindow window = null)
        {
            return totp.VerifyTotp(timestamp, totpCode, out timeWindowUsed, window);
        }
        public int RemainingSeconds()
        {
            return totp.RemainingSeconds();
        }
        public int RemainingSeconds(DateTime timestamp)
        {
            return totp.RemainingSeconds(timestamp);
        }
    }
}
